package com.lge.clock.worldclock.accuweather;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
/**
 * @author lge.com
 */
public class WeatherUtil {

	private static final String TAG = "WeatherUtil";
	
	
	/**
	 * Method isNetworkAvalable.
	 */
	public static boolean isNetworkAvalable(Context context) {
	    ConnectivityManager manager = 
	        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
	    
	    NetworkInfo activeNetInfo = manager.getActiveNetworkInfo();
	    
	    if(activeNetInfo != null && activeNetInfo.isConnected()) {
	        return true;
	    }
	    
        return false;
	}
	
	/**
	 * Method getDaylight.
	 */
	public static int getDaylight(String request) {
		
	    int obsdaylight = -1;
	    
	    TimeZone tz = TimeZone.getTimeZone(request);
		
		boolean mDaylightTime = tz.useDaylightTime();
		
		if(mDaylightTime == true){
			obsdaylight = 1;
		}else{
			
		}


	    return obsdaylight;
	}
	
	public static int getTimeZoneDaylight(String country, String request) {
		
	    int obsdaylight = 0;
	    
	    TimeZone tz = TimeZone.getTimeZone(request);
		Calendar addCityCal = Calendar.getInstance(tz);
		
		addCityCal.setTimeZone(tz);
		
		Date mCurrentDate = null;
		String mCuurentCompareDate = null;
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		df.setTimeZone(tz);
		mCurrentDate = addCityCal.getTime();
		
		mCuurentCompareDate = df.format(mCurrentDate);
		
		boolean  mDaylightTime = tz.inDaylightTime(mCurrentDate);
		
		String mYear = mCuurentCompareDate.substring(0, 4);
		String mMonth = mCuurentCompareDate.substring(5,7);
		String mDay = mCuurentCompareDate.substring(8, 10);
		String mHour = mCuurentCompareDate.substring(11,13);
		String mMinute = mCuurentCompareDate.substring(14, 16);
		
		int mAfterYear = Integer.parseInt(mYear);
		int mAfterMonth = Integer.parseInt(mMonth);
		int mAfterDay = Integer.parseInt(mDay);
		int mAfterHour = Integer.parseInt(mHour);
		int mAfterMinute = Integer.parseInt(mMinute);
		boolean checkCountry = false;
		
		int mSelectedCountryNum = -1;
		for(int i=0;i<TimeZoneTable.mContryTable.length;i++){
			if(TimeZoneTable.mContryTable[i].equals(country)){
				mSelectedCountryNum = i;
				checkCountry = true;
			}
		}
		//[hyunjung78.park][20742] 2011.08.11 Fixed world time error for Casablanca,Rabat city in Morocco START
		if(checkCountry == true){
			for(int j=0;j<TimeZoneTable.mDstTable.length;j++){
				if(mSelectedCountryNum == TimeZoneTable.mDstTable[j][0]){
					if(mAfterYear >= TimeZoneTable.mDstTable[j][1] && mAfterYear <= TimeZoneTable.mDstTable[j][6]){
						int dst_start = (TimeZoneTable.mDstTable[j][1]*12*30)+(TimeZoneTable.mDstTable[j][2]*30) +(TimeZoneTable.mDstTable[j][3]) ; // start day
						int dst_end = (TimeZoneTable.mDstTable[j][6]*12*30)+(TimeZoneTable.mDstTable[j][7]*30) +(TimeZoneTable.mDstTable[j][8]) ; // end day
						int currentTime = (mAfterYear*12*30)+(mAfterMonth*30) +mAfterDay ; // end day
						Log.e(TAG,"dst_start : "+dst_start + ", dst_end : " + dst_end + ", currentTime : " + currentTime); 
						if(currentTime >= dst_start && currentTime <= dst_end){
							int dst_start_minute = (dst_start * 24 * 60) + (TimeZoneTable.mDstTable[j][4] * 60) + TimeZoneTable.mDstTable[j][5];
							int dst_end_minute = (dst_end * 24 * 60) + (TimeZoneTable.mDstTable[j][9] * 60) + TimeZoneTable.mDstTable[j][10];
							int current_minute = (currentTime * 24 * 60)+ (mAfterHour * 60) + mAfterMinute;
							Log.e(TAG,"dst_start_minute : "+dst_start_minute + ", dst_end_minute : " + dst_end_minute + ", current_minute : " + current_minute);
							if(current_minute >= dst_start_minute && current_minute < dst_end_minute){	 
								obsdaylight = 1;
								 
							}
						}	  
					}				 
				}
			}
		}
		else  
		{
			if(mDaylightTime == true){
				if(country.equals(TimeZoneTable.mEgypt)){ 
					obsdaylight = 0;
				}else{
					obsdaylight = 1;
				}
			}
		}
		Log.e(TAG,"obsdaylight : "+obsdaylight );
		//[hyunjung78.park][20742] 2011.08.11 Fixed world time error for Casablanca,Rabat city in Morocco END
	    return obsdaylight;
	}
	
	public static String getGmtString(String gmtValue, String country, String request, int osbDaylight, int mSelectUpdateMode, int updateMode){
		
		// Mode : 0 => Automatic
		//        1 => Disable
		//        2 => Add to One hour
		//        3 => Add to Two hour
		String mDayLight = null;
		
		// Make gmt_value
		String mGmtString = null;
		String mGmtSplit[] = gmtValue.split(" ");
		String mGmtTime = mGmtSplit[2];
		String mGmtSign = mGmtSplit[1];
		String mPlus = "+";
		int mGmtHour = 0;
		int mGmtMinute = 0;
		String mGmtHourString = null;
		String mGmtMinuteString = null;
		int mGmtTimeSize = mGmtTime.length();
		if(mGmtTimeSize > 3){
			String mGmtSplitMinute[] = mGmtTime.split(":");
			mGmtHour = Integer.parseInt(mGmtSplitMinute[0]);
			mGmtMinute = Integer.parseInt(mGmtSplitMinute[1]);
		}else{
			mGmtHour = Integer.parseInt(mGmtTime);
		}
		int mNewDayLight = getTimeZoneDaylight(country, request);
		if(mSelectUpdateMode == 0){
			if(updateMode == 1){
				if(mNewDayLight == 1){
					if(mGmtSign.equals(mPlus)){
						mGmtHour += 1;
					}else{
						mGmtHour -= 1;
					}
				}
			}else if(updateMode == 2){
				if(osbDaylight == 1){
					if(mNewDayLight == 0){
						if(mGmtSign.equals(mPlus)){
							mGmtHour -= 1;
						}else{
							mGmtHour += 1;
						}
					}
				}
			}else if(updateMode == 3){
				if(mNewDayLight == 1){
					if(mGmtSign.equals(mPlus)){
						mGmtHour -= 1;
					}else{
						mGmtHour += 1;
					}
				}else{
					if(mGmtSign.equals(mPlus)){
						mGmtHour -= 2;
					}else{
						mGmtHour += 2;
					}
				}
			}
			osbDaylight = mNewDayLight;
		}else if(mSelectUpdateMode == 1){
			if(updateMode == 0){
				if(osbDaylight == 1){
					if(mGmtSign.equals(mPlus)){
						mGmtHour -= 1;
					}else{
						mGmtHour += 1;
					}
				}
			}else if(updateMode == 2){
				if(mGmtSign.equals(mPlus)){
					mGmtHour -= 1;
				}else{
					mGmtHour += 1;
				}
			}else if(updateMode == 3){
				if(mGmtSign.equals(mPlus)){
					mGmtHour -= 2;
				}else{
					mGmtHour += 2;
				}
			}
			osbDaylight = 0;
		}else if(mSelectUpdateMode == 2){
			if(updateMode == 0){
				if(osbDaylight == 0){
					if(mGmtSign.equals(mPlus)){
						mGmtHour += 1;
					}else{
						mGmtHour -= 1;
					}
				}
			}else if(updateMode == 1){
				if(mGmtSign.equals(mPlus)){
					mGmtHour += 1;
				}else{
					mGmtHour -= 1;
				}
			}else if(updateMode == 3){
				if(mGmtSign.equals(mPlus)){
					mGmtHour -= 1;
				}else{
					mGmtHour += 1;
				}
			}
			osbDaylight = 1;
		}else if(mSelectUpdateMode == 3){
			if(updateMode == 0){
				if(osbDaylight == 0){
					if(mGmtSign.equals(mPlus)){
						mGmtHour += 2;
					}else{
						mGmtHour -= 2;
					}
				}else{
					if(mGmtSign.equals(mPlus)){
						mGmtHour += 1;
					}else{
						mGmtHour -= 1;
					}
				}
			}else if(updateMode == 1){
				if(mGmtSign.equals(mPlus)){
					mGmtHour += 2;
				}else{
					mGmtHour -= 2;
				}
			}else if(updateMode == 2){
				if(mGmtSign.equals(mPlus)){
					mGmtHour += 1;
				}else{
					mGmtHour -= 1;
				}
			}
			osbDaylight = 2;
		}
		
//		if(mSelectUpdateMode == 0){
//			if(updateMode == 1){
//				if(mGmtSign.equals(mPlus)){
//					mGmtHour -= 1;
//				}else{
//					mGmtHour += 1;
//				}
//			}else if(updateMode == 2){
//				if(mGmtSign.equals(mPlus)){
//					mGmtHour -= 2;
//				}else{
//					mGmtHour += 2;
//				}
//			}
//			osbDaylight = 0;
//		}else if(mSelectUpdateMode == 1){
//			if(updateMode == 0){
//				if(mGmtSign.equals(mPlus)){
//					mGmtHour += 1;
//				}else{
//					mGmtHour -= 1;
//				}
//			}else if(updateMode == 2){
//				if(mGmtSign.equals(mPlus)){
//					mGmtHour -= 1;
//				}else{
//					mGmtHour += 1;
//				}
//			}
//			osbDaylight = 1;
//		}else if(mSelectUpdateMode == 2){
//			if(updateMode == 0){
//				if(mGmtSign.equals(mPlus)){
//					mGmtHour += 2;
//				}else{
//					mGmtHour -= 2;
//				}
//			}else if(updateMode == 1){
//				if(mGmtSign.equals(mPlus)){
//					mGmtHour += 1;
//				}else{
//					mGmtHour -= 1;
//				}
//			}
//			osbDaylight = 2;
//		}
		
		mDayLight = Integer.toString(osbDaylight);
		if(mGmtMinute == 0){
			mGmtHourString = Integer.toString(mGmtHour);
			mGmtString = mGmtSplit[0] + " " + mGmtSign + " " + mGmtHourString + "/" + mDayLight;
		}else{
			mGmtHourString = Integer.toString(mGmtHour);
			mGmtMinuteString = Integer.toString(mGmtMinute);
			mGmtString = mGmtSplit[0] + " " + mGmtSign + " " + mGmtHourString + ":" + mGmtMinuteString + "/" + mDayLight;
		}
		return mGmtString;
	}
}

